package com.guaju.yueshangLife.adapter;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.bean.ServiceMsg;
import com.guaju.yueshangLife.databinding.ItemMineMsgBinding;
import com.guaju.yueshangLife.databinding.ItemServiceMsgBinding;

import java.util.List;

public class ServiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<ServiceMsg> list;
    private int status = 1;

    public static final int SENDER_MINE = 8801;
    public static final int SENDER_SERVICE = 8802;

    public static final int TYPE_TEXT = 881;
    public static final int TYPE_PIC = 882;

    Activity activity;
    private final LayoutInflater layoutInflater;
    private View view;

    public ServiceAdapter(Context context, List<ServiceMsg> list) {
        this.context = context;
        this.list = list;
        activity = (Activity) context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {

        //选择类型
        RecyclerView.ViewHolder result = null;
        switch (type) {
            case SENDER_MINE:
                ItemMineMsgBinding itemMineMsgBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_mine_msg, parent, false);
                result = new MineMsgHolder(itemMineMsgBinding.getRoot());
                break;
            case SENDER_SERVICE:
                ItemServiceMsgBinding itemServiceMsgBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_service_msg, parent, false);
                result = new ServiceMsgHolder(itemServiceMsgBinding.getRoot());
                break;

        }
        return result;
    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        if (holder instanceof MineMsgHolder) {
            MineMsgHolder mineMsgHolder = (MineMsgHolder) holder;
            mineMsgHolder.setContent(list.get(position));
        } else if (holder instanceof ServiceMsgHolder) {
            ServiceMsgHolder h = (ServiceMsgHolder) holder;
            h.setContent(list.get(position));

        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return list.size();
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        ServiceMsg o = list.get(position);
        return o.getMsgSender();
    }
}


class MineMsgHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ItemMineMsgBinding itemMineMsgBinding;

    public MineMsgHolder(View itemView) {
        super(itemView);
        itemMineMsgBinding = DataBindingUtil.getBinding(itemView);
        itemView.setOnClickListener(this);

    }

    public void setContent(ServiceMsg serviceMsg) {
        itemMineMsgBinding.sdvMine.setImageURI(serviceMsg.getAvatar());
        if (ServiceAdapter.TYPE_TEXT==serviceMsg.getMsgType()){
            itemMineMsgBinding.tvMineMsg.setVisibility(View.VISIBLE);
            itemMineMsgBinding.sdvMineMsg.setVisibility(View.GONE);
            itemMineMsgBinding.tvMineMsg.setText(serviceMsg.getMsg());
            itemMineMsgBinding.tvMineMsg.setTextColor(Color.parseColor("#FFFFFF"));

        }else if (ServiceAdapter.TYPE_PIC==serviceMsg.getMsgType()){
            itemMineMsgBinding.tvMineMsg.setVisibility(View.GONE);
            itemMineMsgBinding.sdvMineMsg.setVisibility(View.VISIBLE);
            itemMineMsgBinding.sdvMine.setImageURI(serviceMsg.getMsgImageUrl());
        }
    }

    @Override
    public void onClick(View v) {
    }
}

class ServiceMsgHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ItemServiceMsgBinding itemServiceMsgBinding;

    public ServiceMsgHolder(View itemView) {
        super(itemView);
        itemServiceMsgBinding = DataBindingUtil.getBinding(itemView);
        itemView.setOnClickListener(this);

    }

    public void setContent(ServiceMsg serviceMsg) {
        itemServiceMsgBinding.sdvService.setImageURI(serviceMsg.getAvatar());
        itemServiceMsgBinding.tvServiceMsg.setText(serviceMsg.getMsg());
        itemServiceMsgBinding.tvServiceMsg.setTextColor(Color.parseColor("#333333"));
    }

    @Override
    public void onClick(View v) {

    }
}